package com.admin.service;

import java.sql.SQLException;
import java.util.List;

import net.sf.ehcache.Element;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.bean.BillingBean;
import com.admin.bean.MobileProductBean;
import com.admin.cache.EhCacheManager;
import com.admin.dao.BillingDao;
import com.admin.dao.BillingSpDao;
import com.admin.dao.MobileProductDao;

@Service
public class BillingService {
	
	private static final Logger log = Logger.getLogger(BillingService.class);

	@Autowired private BillingDao billingDao;
	@Autowired private BillingSpDao billingSpDao;
	@Autowired private MobileProductDao mobileProductDao;
	
	/**
	 * 添加新的计费点
	 * @param billing
	 * @return
	 */
	public int insertBilling(BillingBean billing){
		return billingDao.insertBilling(billing);
	}
	
	/**
	 * 删除计费点
	 * @param billingId
	 * @return
	 */
	public int delBilling(int billingId){
		try{
			billingSpDao.delByBillingId(billingId);
			billingDao.delBilling(billingId);
			EhCacheManager.getCache().remove(EhCacheManager.BILLING+billingId);
			return 1;
		}catch(RuntimeException e){
			return 0;
		}
	}
	
	/**
	 * 修改计费点
	 * @param billing
	 * @return
	 */
	public int updateBilling(BillingBean billing){
		int count = billingDao.updateBilling(billing);
		if(count>0){
			EhCacheManager.putValue(new Element(EhCacheManager.BILLING+billing.getBillingId(),billing));
		}
		return count;
	}
	
	/**
	 * 根据条件查询计费总数
	 * @param productId
	 * @param userType
	 * @return
	 */
	public int findCountByCondition(int productId,int userType){
		return billingDao.findCountByCondition(productId, userType);
	}
	
	/**
	 * 根据条件查询计费点
	 * @param productId
	 * @param userType
	 * @param start
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
	public List<BillingBean> findBillingByCondition(int productId,int userType,int start,int pageSize) {
		try {
			return billingDao.findBillingByCondition(productId, userType, start, pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据ID查询
	 * @param billingId
	 * @return
	 * @throws SQLException
	 */
	public BillingBean findBillingById(int billingId){
		try {
			String key = EhCacheManager.BILLING+billingId;
			Element e = EhCacheManager.getValue(key);
			BillingBean billing = null;
			if(e==null){
				billing = billingDao.findBillingById(billingId); 
				EhCacheManager.putValue(new Element(key, billing));
			}else{
				billing = (BillingBean)e.getValue();
			}
			return billing;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据产品ID取计费点
	 * @param productId
	 * @return
	 * @throws SQLException
	 */
	public List<BillingBean> findBillingByProductId(int productId){
		try {
			return billingDao.findBillingByProductId(productId);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 修改计费点中的产品名
	 * @param productId
	 * @return
	 */
	public int updateProductName(int productId,String productName){
		int count = billingDao.updateProductName(productId, productName);
		if(count>0){
			try {
				List<BillingBean> billingList = billingDao.findBillingByProductId(productId);
				for (BillingBean billing : billingList) {
					EhCacheManager.putValue(new Element(EhCacheManager.BILLING+billing.getBillingId(), billing));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}
	
	/**
	 * 根据CID去计费点ID
	 * @param cid
	 * @return
	 */
	public int getFeeIdByCID(int cid){
		try {
			MobileProductBean mp = mobileProductDao.findFirstByMobileTypeId(cid);
			if (mp == null) {
				log.error("未绑定产品!");
				return 0;
			}
			List<BillingBean> billingList = findBillingByProductId(mp.getProductId());
			int feeId = 0;
			if (billingList != null && billingList.size() > 0) {
				feeId = billingList.get(0).getBillingId();
			}else{
				log.error("该产品未配置计费点！");
			}
			return feeId;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		
	}
}
